﻿
using Microsoft.SPOT;
using System;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;





namespace Microsoft.NETMF.BikeComputer.Application
{
    
    public class Program : Microsoft.SPOT.Application
    {

        public static void Main()
        {
            if (SystemMetrics.ScreenHeight != 272)
            {
                DisplaySetup();
               // PowerState.RebootDevice(true);   //reboot the device to have the configuration changes take effect.  This is dangerous if the Display Setup fails and it is predicated on a poor 'If' test.
            }
            Program myApplication = new Program();

            RideDataModel model = new RideDataModel();

            ComputerController controller = new ComputerController(myApplication, model);

            ReadyView readyView = new ReadyView(model);
            SettingsView settingsView = new SettingsView(model, controller);
            RideView rideView = new RideView(model,controller);
            DataView dataView = new DataView(model);
            SummaryView summaryView = new SummaryView(model);
            ZeroInclinometerView zeroInclinometerView = new ZeroInclinometerView(model, controller);

            Window mainWindow = myApplication.CreateWindow(readyView);

            controller.AttachView(readyView, settingsView, rideView, dataView, summaryView, zeroInclinometerView, mainWindow);
            controller.Start(readyView);

            // Start the application
            myApplication.Run(mainWindow);
        }

        
        private Window CreateWindow(UIElement view)
        {
            _mainWindow = new Window();
            _mainWindow.Height = SystemMetrics.ScreenHeight;
            _mainWindow.Width = SystemMetrics.ScreenWidth;


            _mainWindow.Background = new SolidColorBrush(ColorUtility.ColorFromRGB(200, 250, 200));

            _mainWindow.Child = view;

            _mainWindow.Visibility = Visibility.Visible;

            return _mainWindow;

        }

        private static void DisplaySetup()
        {
            Configuration.LCD.Configurations _lcdConfiguration = new Configuration.LCD.Configurations();

            _lcdConfiguration.Width = 480;
            _lcdConfiguration.Height = 272;

            // Only use if needed, see documentation.
            _lcdConfiguration.PriorityEnable = false;

            _lcdConfiguration.OutputEnableIsFixed = true;
            _lcdConfiguration.OutputEnablePolarity = true;

            _lcdConfiguration.HorizontalSyncPolarity = false;
            _lcdConfiguration.VerticalSyncPolarity = false;
            _lcdConfiguration.PixelPolarity = false;

            _lcdConfiguration.HorizontalSyncPulseWidth = 41;
            _lcdConfiguration.HorizontalBackPorch = 2;
            _lcdConfiguration.HorizontalFrontPorch = 2;
            _lcdConfiguration.VerticalSyncPulseWidth = 10;
            _lcdConfiguration.VerticalBackPorch = 2;
            _lcdConfiguration.VerticalFrontPorch = 2;

            // NOTE: This is used for EMX, comment if using ChipworkX.
            _lcdConfiguration.PixelClockDivider = 8;

            // NOTE: This is used for ChipworkX, comment if using EMX.
            //lcdConfig.PixelClockDivider = 4;

            // Set configs
            Configuration.LCD.Set(_lcdConfiguration);

            //read EWR touch configuration


        }

        struct point
        {
            public int x;
            public int y;
        }

        [Serializable]
        internal sealed class TouchCalibrationData
        {


        }



        private Window _mainWindow;
       // private static ExtendedWeakReference _touchCalibrationData;

    }


}
